<?php
namespace app\m\controller;

use app\common\controller\HomeBase;
use Firebase\JWT\JWT;
use org\SendMsg;
use org\SiteMap;
use TencentCloud\Captcha\V20190722\CaptchaClient;
use TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use think\facade\Cache;
use think\facade\Config;
use think\helper\Time;
use wechat\WechatAuth;
use app\common\model\User as UserModel;

class Index extends HomeBase
{
    public function index()
    {
        $this->assign('index_style',"home");
        $this->site_seo("home");
        return $this->fetch();
    }

    /**
     * 分类
     * @return mixed
     */
    public function category()
    {
        $this->assign('index_style',"category");
        $this->site_seo("home");
        return $this->fetch();
    }


    /**
     * 文档下载
     */
    public function download()
    {

        if (!empty($this->user_info)) {
            if($this->user_info['vip_id'] > 0) {
                $id = $this->request->param('id');

                $vip = model('vip')->where(['id' => $this->user_info['vip_id']])->find();
                $timeArr = Time::today();
                $downNum = model('down_log')->where(['user_id' => $this->user_info['id']])->where('create_time','gt',$timeArr[0])->where('create_time','lt',$timeArr[1])->count();

                if ($downNum > $vip['down_num'] ){
                    $this->echoJson('',-1,"下载次数超过".$vip['down_num']."次,请明日在下载!");
                }else {
                    $item = model('article')->where(['id' => $id])->find();

                    if (empty($item))
                    {
                        return $this->error("文件不存在");
                    }

                    $data = curl_url($this->siteConfig['interface_url'] . "/get_sign_url",['file_name' => $item['file_name'],'bid' => $item['bid']]);

                    $data = json_decode($data['data'],true);

                    if (model('down_log')->where(['user_id' =>  $this->user_info['id']])->where(['doc_id' => $id])->count() == 0) {
                        model('down_log')->insert([
                            'title' => $item['title'],
                            'user_id' => $this->user_info['id'],
                            'doc_id' => $id,
                            'create_time' => time(),
                        ]);
                    }

                    $this->echoJson($data['data'],200,"获取成功!");
                }

            }else {
                $this->echoJson("",-1,"请先开通会员!");
            }
        }else{
            $this->echoJson("",-1,"用户未登录");
        }
    }


    /**
     * 站点地图
     */
    public function sitemap ()
    {
        $full_list = model("article")->where(["status" => 1])->limit(20)->order('sort','desc')->select();
        $site = new Sitemap();

        foreach ($full_list as $item)
        {
            $url = \request()->domain(). "/zy-" . $item['id'] . '.html';
            $site->AddItem($url, 0,'daily',$item['create_time']);
        }

        $site->Show();
    }

}
